package com.example.mycarddemo4.widget.player;

import com.example.mycarddemo4.MusicPlayerManager;
import com.example.mycarddemo4.widget.controller.FormController;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.FormBindingData;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.ability.ProviderFormInfo;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONObject;

/**
 * The PlayerImpl
 */
public class PlayerImpl extends FormController {

    private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, PlayerImpl.class.getName());

    private static final String GET_DETAIL_INFO = "get_detail_info";

    private static final String SET_PLAY_MODE = "set_play_mode";

    private static final String GET_PREVIOUS = "get_previous";

    private static final String PLAY = "play";
    private static final String PAUSE = "pause";
    private static final String GET_NEXT = "get_next";

    private static final String GET_FAVORITE = "get_favorite";

    private final Context mContext;
    private MusicPlayerManager mPlayerManager;
    public PlayerImpl(Context context, String formName, Integer dimension) {
        super(context, formName, dimension);
        mContext = context;
        mPlayerManager = new MusicPlayerManager(context);
        mPlayerManager.initMusicList(context,
                new String[]{"resources/rawfile/demo.wav", "resources/rawfile/dynamic.wav"});
    }

    @Override
    public ProviderFormInfo bindFormData() {
        ZSONObject data = new ZSONObject();
        data.put("songName", "点击播放");
        FormBindingData bindingData = new FormBindingData(data);
        ProviderFormInfo info = new ProviderFormInfo();
        info.setJsBindingData(bindingData);
        return info;
    }

    @Override
    public void updateFormData(long formId, Object... vars) {
    }

    @Override
    public void onTriggerFormEvent(long formId, String message) {
        ZSONObject zsonObject = ZSONObject.stringToZSON(message);

        // Do something here after receive the message from js card
        ZSONObject result = new ZSONObject();
        String songName = null;
        switch (zsonObject.getString("mAction")) {
            case GET_DETAIL_INFO:
                result.put("itemTitle", GET_DETAIL_INFO);
                result.put("itemContent", "you clicked get detail info");
                break;
            case SET_PLAY_MODE:
                result.put("itemTitle", SET_PLAY_MODE);
                result.put("itemContent", "you clicked set play mode");
                break;
            case GET_PREVIOUS:
                songName = mPlayerManager.playPreviousMusic(context);
                if (songName == null) {
                    HiLog.info(TAG,"没歌曲了");
                    return;
                }
                result.put("isPause", false);
                result.put("songName", songName);
                break;
            case PLAY:
                result.put("itemContent", "you clicked play");
                result.put("isPause", false);
                songName = mPlayerManager.playMusic();
                result.put("songName", songName);
                break;
            case PAUSE:
                result.put("isPause", true);
                result.put("songName", "点击播放");
                mPlayerManager.pauseMusic();
                break;
            case GET_NEXT:
                songName = mPlayerManager.playNextMusic(mContext);
                if (songName == null) {
                    HiLog.info(TAG,"没歌曲了");
                    return;
                }
                result.put("isPause", false);
                result.put("songName", songName);
                break;
            case GET_FAVORITE:
                result.put("itemTitle", GET_FAVORITE);
                result.put("itemContent", "you clicked get favorite");
                break;
            default:
                break;
        }

        // Update js card
        try {
            if (mContext instanceof Ability) {
                ((Ability) mContext).updateForm(formId, new FormBindingData(result));
            }
        } catch (FormException e) {
            HiLog.error(TAG, e.getMessage());
        }
    }

    @Override
    public Class<? extends AbilitySlice> getRoutePageSlice(Intent intent) {
        return null;
    }
}
